<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="服务类别" prop="methodInfo">
             <el-select v-model="queryParams.methodInfo" placeholder="服务类别" clearable size="small">
               <el-option
                 v-for="dict in methodInfoOptions"
                 :key="dict.dictValue"
                 :label="dict.dictLabel"
                 :value="dict.dictValue"
               />
             </el-select>
           </el-form-item>
	  <el-form-item label="API" prop="memoInfo">
        <el-input
          v-model="queryParams.memoInfo"
          placeholder="API名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>
	  <el-form-item>
	    <el-col><a href="http://qxsdcloud.com/wiki/doc/xOSS9Wr2F" target="_blank"><font size="5px"color="#000FFF">API接口使用指南</font></a></el-col>
	  </el-form-item>
	  <el-form-item>
	    <el-col>微信：<font size="5px"color="#00aa00">yabushan</font></a></el-col>
	  </el-form-item>
    </el-form>
<el-collapse style="font-size: 100px;">
   <el-collapse-item :key="item.keyId" v-for="(item,index) in aliApiList"
					   :title="item.memoInfo" :name="index" >
					 <div><span style="margin-left: 30px;">key：<label style="color: #005500;">{{item.keyId}}</label></span>  </div>
		    <div><span style="margin-left: 30px;">服务提供商：<label style="color: #0000FE;">{{item.serverWay}}</label></span>  
			<span style="margin-left: 30px;">服务类别：<label style="color: #ff0000;">{{item.methodInfo}}</label> </span>
			<span style="margin-left: 30px;">  发布时间：<label style="color: #ffaa00;">{{ parseTime(item.createdTime, '{y}-{m}-{d}') }}</label></span>
			<span style="margin-left: 30px;">总请求次数：<label style="color: #fe6a26;">{{item.reqNum}} </label></span>
			</div>
		 <hr color="#00ff7f" />
		  <el-button type="primary" ><a :href="item.userWay" target="_blank">API测试工具</a></el-button >
	</el-collapse-item>
</el-collapse>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listAliApi, getAliApi, delAliApi, addAliApi, updateAliApi, exportAliApi } from "@/api/system/aliApi";

export default {
  name: "AliApi",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 聚合数据表格数据
      aliApiList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 服务类别,METHOD_INFO字典
      methodInfoOptions: [],
      // CONTENT_TYPE字典
      contentTypeOptions: [],
      // 状态，Y：启用，N：停用字典
      apiStatusOptions: [],
      // 是否只返回body,Y:是，N:不是字典
      returnBodyOptions: [],
      // 服务提供商字典
      serverWayOptions: [],
      // 是否被授权，Y:是，N：自己的字典
      isGrantOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        hostInfo: null,
        methodInfo: null,
        pathInfo: null,
        appCode: null,
        contentType: null,
        createdBy: null,
        createdTime: null,
        apiStatus: null,
        updatedTime: null,
        updatedBy: null,
        returnBody: null,
        levelNum: null,
        serverWay: null,
        reqNum: null,
        memoInfo: null,
        isGrant: '1',
        userWay: null,
        ownerName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("METHOD_INFO").then(response => {
      this.methodInfoOptions = response.data;
    });
    this.getDicts("CONTENT_TYPE").then(response => {
      this.contentTypeOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.apiStatusOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.returnBodyOptions = response.data;
    });
    this.getDicts("SERVER_WAY").then(response => {
      this.serverWayOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.isGrantOptions = response.data;
    });
  },
  methods: {
    /** 查询聚合数据列表 */
    getList() {
      this.loading = true;
      listAliApi(this.queryParams).then(response => {
        this.aliApiList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 服务类别,METHOD_INFO字典翻译
    methodInfoFormat(row, column) {
      return this.selectDictLabel(this.methodInfoOptions, row.methodInfo);
    },
    // CONTENT_TYPE字典翻译
    contentTypeFormat(row, column) {
      return this.selectDictLabel(this.contentTypeOptions, row.contentType);
    },
    // 状态，Y：启用，N：停用字典翻译
    apiStatusFormat(row, column) {
      return this.selectDictLabel(this.apiStatusOptions, row.apiStatus);
    },
    // 是否只返回body,Y:是，N:不是字典翻译
    returnBodyFormat(row, column) {
      return this.selectDictLabel(this.returnBodyOptions, row.returnBody);
    },
    // 服务提供商字典翻译
    serverWayFormat(row, column) {
      return this.selectDictLabel(this.serverWayOptions, row.serverWay);
    },
    // 是否被授权，Y:是，N：自己的字典翻译
    isGrantFormat(row, column) {
      return this.selectDictLabel(this.isGrantOptions, row.isGrant);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        keyId: null,
        hostInfo: null,
        methodInfo: null,
        pathInfo: null,
        appCode: null,
        contentType: null,
        createdBy: null,
        createdTime: null,
        apiStatus: null,
        updatedTime: null,
        updatedBy: null,
        returnBody: null,
        levelNum: null,
        serverWay: null,
        reqNum: null,
        memoInfo: null,
        isGrant: null,
        userWay: null,
        ownerName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.keyId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加聚合数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const keyId = row.keyId || this.ids
      getAliApi(keyId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改聚合数据";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.keyId != null) {
            updateAliApi(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addAliApi(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const keyIds = row.keyId || this.ids;
      this.$confirm('是否确认删除聚合数据编号为"' + keyIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delAliApi(keyIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有聚合数据数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportAliApi(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
